package com.finance.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finance.system.user.enums.GroupType;
import com.finance.system.support.BaseServiceImpl;
import com.finance.system.support.bean.OffsetQuery;
import com.finance.system.support.bean.ResultBean;
import com.finance.system.support.bean.ResultConstant;
import com.finance.system.support.utils.DomainTransHelper;
import com.finance.system.user.entity.GroupEntity;
import com.finance.system.user.entity.GroupInbound;
import com.finance.system.user.entity.UserEntity;
import com.finance.system.user.entity.UserGroupRelationEntity;
import com.finance.system.user.mapper.UserGroupRelationMapper;
import com.finance.system.user.service.IGroupService;
import com.finance.system.user.service.IUserGroupRelationService;
import com.finance.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whdong
 * @since 2019-10-11
 */
@Service
public class UserGroupRelationServiceImpl extends BaseServiceImpl<UserGroupRelationMapper, UserGroupRelationEntity> implements IUserGroupRelationService {

    @Autowired
    private IGroupService groupService;
    @Autowired
    private IUserService userService;

    @Override
    public List<UserGroupRelationEntity> getByUserId(String userId, OffsetQuery<UserGroupRelationEntity> offsetQuery) {
        QueryWrapper<UserGroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        if(offsetQuery == null){
            return baseMapper.selectList(queryWrapper);
        }
        return baseMapper.selectPage(offsetQuery,queryWrapper).getRecords();
    }

    @Override
    public List<UserGroupRelationEntity> getByGroupId(String groupId,
                                                      OffsetQuery<UserGroupRelationEntity> offsetQuery) {
        QueryWrapper<UserGroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id",groupId);
        if(offsetQuery == null){
            return baseMapper.selectList(queryWrapper);
        }
        return baseMapper.selectPage(offsetQuery,queryWrapper).getRecords();
    }

    @Override
    public ResultBean<List<GroupEntity>> getGroupByUserId(String userId) {
        if(StringUtils.isEmpty(userId)){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE, Collections.emptyList());
        }
        List<UserGroupRelationEntity> relations = this.getByUserId(userId,null);
        List<Long> groupIds =
                relations.stream().map(UserGroupRelationEntity::getGroupId).collect(Collectors.toList());
        Collection<GroupEntity> groups = groupService.listByIds(groupIds);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,
                new ArrayList<>(groups));

    }

    @Override
    public ResultBean<List<UserEntity>> getUserByGroupId(String groupId) {
        if(StringUtils.isEmpty(groupId)){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE, Collections.emptyList());
        }
        List<UserGroupRelationEntity> relations = this.getByGroupId(groupId,null);
        if(CollectionUtils.isEmpty(relations)){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE, Collections.emptyList());
        }
        Collection<UserEntity> users =
                userService.listByIds(relations.stream().map(UserGroupRelationEntity::getUserId).collect(Collectors.toList()));

        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,
                new ArrayList<>(users));
    }

    @Override
    public ResultBean<GroupEntity> updateGroup(GroupInbound group) {
        if(group.getId() == null){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,null);
        }
        GroupEntity groupEntity = DomainTransHelper.transFromTo(group, GroupEntity.class);
        groupService.updateById(groupEntity);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,groupEntity);
    }

    @Override
    public ResultBean<GroupEntity> addOne(GroupInbound group) {
        if(StringUtils.isEmpty(group.getCreater())){
            return ResultBean.create(ResultConstant.ERROR_CODE,"创建者不能为空",null);
        }
        //创建新的账本
        GroupEntity newGroup = addNewGroup(group);
        //建立用户账本关系
        UserGroupRelationEntity relation = addNewGroupRelation(newGroup);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.ADD_SUCCESS_MESSAGE,
                newGroup);
    }

    @Override
    public ResultBean addUserInToGroup(UserGroupRelationEntity relation) {
        if(StringUtils.isEmpty(relation.getGroupId())){
            return ResultBean.create(ResultConstant.ERROR_CODE,ResultConstant.ERROR_MESSAGE_NO_GROUP_ID,null);
        }
        if(StringUtils.isEmpty(relation.getUserId())){
            return ResultBean.create(ResultConstant.ERROR_CODE,ResultConstant.ERROR_MESSAGE_NO_USER_ID,null);
        }
        this.save(relation);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.ADD_SUCCESS_MESSAGE,null);
    }

    private UserGroupRelationEntity addNewGroupRelation(GroupEntity newGroup) {
        UserGroupRelationEntity entity = new UserGroupRelationEntity();
        entity.setGroupId(newGroup.getId());
        entity.setUserId(newGroup.getCreater());
        this.save(entity);
        return entity;
    }

    private GroupEntity addNewGroup(GroupInbound group) {
        GroupEntity groupEntity = DomainTransHelper.transFromTo(group, GroupEntity.class);
        groupEntity.setGroupType(GroupType.NORMAL.name());
        groupService.save(groupEntity);
        return groupEntity;
    }
}
